package com.jsj.controller;

import com.alibaba.fastjson2.JSONObject;
import com.jsj.domain.Problem;
import com.jsj.domain.TopClass;
import com.jsj.domain.Topic;
import com.jsj.service.TopicService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.config.RabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/topic")
public class TopicController {
    private TopicService topicService;

    @Autowired

    public void setTopicService(TopicService topicService) {
        this.topicService = topicService;
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 查询班级作业正在使用
     */
    @RequestMapping(value = "/queryByClassNow/{classId}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public List<Topic> queryByClassNow(@PathVariable("classId") int classId) {
        startPage();
        return topicService.queryByClassNow(classId);
    }

    /**
     * 查询班级作业已过期
     */
    @RequestMapping(value = "/queryByClassAfter/{classId}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public List<Topic> queryByClassAfter(@PathVariable("classId") int classId) {
        startPage();
        return topicService.queryByClassAfter(classId);
    }

    /**
     * 查询班级作业未开始
     */
    @RequestMapping(value = "/queryByClassBefore/{classId}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public List<Topic> queryByClassBefore(@PathVariable("classId") int classId) {
        startPage();
        return topicService.queryByClassBefore(classId);
    }

    /**
     * 查询班级作业
     */
    @RequestMapping(value = "/queryByClass/{classId}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public List<Topic> queryByClass(@PathVariable("classId") int classId) {
        startPage();
        return topicService.queryByClass(classId);
    }

    /**
     * 根据教师查询班级作业
     */
    @RequestMapping(value = "/queryByTeacher/{teacherId}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public List<Topic> queryByTeacher(@PathVariable("teacherId") int teacherId) {
        startPage();
        return topicService.queryByTeacher(teacherId);
    }

    /**
     * 根据教师name查询班级作业
     */
    @RequestMapping(value = "/queryByCreator/{creator}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public List<Topic> queryByCreator(@PathVariable("creator") String creator) {
        startPage();
        return topicService.queryByCreator(creator);
    }
    /**
     * 根据作业名查询班级作业
     */
    @RequestMapping(value = "/queryByTitle/{title}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public List<Topic> queryByTitle(@PathVariable("title") String title) {
        startPage();
        return topicService.queryByTitle(title);
    }

    /**
     * 查询作业
     */
    @RequestMapping(value = "/queryById/{id}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public Topic queryById(@PathVariable("id") int id) {
        startPage();
        return topicService.queryById(id);
    }

    /**
     * 创建作业
     */
    @RequestMapping(value = "/insert", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult insert(@RequestBody Topic topic) {
        if (topicService.insert(topic) == 0) {
            return AjaxResult.error("创建" + topic.getTitle() + "作业失败");
        }
        return AjaxResult.success("创建" + topic.getTitle() + "作业成功");
    }

    /**
     * 删除作业
     */
    @RequestMapping(value = "/delete/{id}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult delete(@PathVariable("id") int id) {
        if (topicService.delete(id) == 0) {
            return AjaxResult.error("删除作业失败");
        }
        return AjaxResult.success("删除作业成功");
    }

//    /**
//     * 发布作业
//     */
//    @RequestMapping(value = "/allocateForClass", method = {RequestMethod.POST})
//    @ResponseBody
//    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
//    public AjaxResult allocateForClass(@RequestBody int[] id) {
//        if (topicService.allocateForClass(id[0], id[1]) == 0) {
//            return AjaxResult.error("发布作业失败");
//        }
//        return AjaxResult.success("发布作业成功");
//    }
    /**
     * 发布作业
     */
    @RequestMapping(value = "/allocateForClass1", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult allocateForClass(@RequestBody TopClass topClass) {
        if (topicService.allocateForClass1(topClass) == 0) {
            return AjaxResult.error("发布作业失败");
        }
        try{
            List<String> usrId=topicService.selectPushList(topClass.getClassId());
            System.out.println(usrId);
            rabbitTemplate.convertAndSend(RabbitConfig.SIMPLE_QUEUE_NAME, topClass.getTopicId());
        }
        catch (Exception e){
            System.out.println("推送失败："+e);
        }
        finally {
            return AjaxResult.success("发布作业成功");
        }
    }

    /**
     * 关闭作业
     */
    @RequestMapping(value = "/unAssignForClass", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult unAssignForClass(@RequestBody int[] id) {
        if (topicService.unAssignForClass(id[0], id[1]) == 0) {
            return AjaxResult.error("关闭作业失败");
        }
        return AjaxResult.success("关闭作业成功");
    }

    /**
     * 修改作业
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult update(@RequestBody Topic topic) {
        if (topicService.update(topic) == 0) {
            return AjaxResult.error("修改作业失败");
        }
        return AjaxResult.success("修改作业成功");
    }

    /**
     * 查询作业
     */
    @RequestMapping(value = "/queryByTopic/{topicId}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public int queryClassByTopic(@PathVariable("topicId") int topicId) {
        startPage();
        return topicService.queryClassByTopic(topicId);
    }

    /**
     * 查询作业
     */
    @RequestMapping(value = "/queryByFileName/{fileName}", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public int queryByFileName(@PathVariable("fileName") String fileName) {
        startPage();
        return topicService.queryByFileName(fileName);
    }
    /**
     * 修改作业
     */
    @RequestMapping(value = "/checkmyself", method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('teacher,admin')")
    public AjaxResult checkmyself(@RequestBody Topic topic) {
        System.out.println(topicService.checkmyself(topic));
        if (topicService.checkmyself(topic) == 0) {
            return AjaxResult.error("0");
        }
        return AjaxResult.success("1");
    }
    @RequestMapping(value = "/image",method = {RequestMethod.POST})
    @ResponseBody
    @PreAuthorize("@ss.hasAnyRoles('student,teacher,admin')")
    public String ImageToOss(@RequestParam("file") MultipartFile file){
        JSONObject jsonObject=new JSONObject();
        if (file != null) {
            jsonObject.put("errno","0");
            JSONObject jsonObject1=new JSONObject();
            jsonObject1.put("url","C:/Users/D/Pictures/Saved%20Pictures/%E6%9D%8E%E5%B8%85%E6%96%8C2.jpg");
            jsonObject1.put("alt","not");
            jsonObject.put("data",jsonObject1);
            return jsonObject.toString();
        }
        else{
            jsonObject.put("errno","1");
            jsonObject.put("message","lalala");
            return jsonObject.toString();
        }
    }

}
